package com.dhemery.mack.actions.tests;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.junit.Before;
import org.junit.Test;

import com.dhemery.mack.actions.SaveAction;
import com.dhemery.mack.adapters.ImageDescriptorSource;
import com.dhemery.mack.adapters.tests.FakeConstantImageDescriptorSource;
import com.dhemery.mack.adapters.tests.FakeImageDescriptor;

import static org.mockito.Mockito.*;

public class SaveActionTests {
	private ImageDescriptorSource imageDescriptorSource;

	@Before
	public void setup() {
		ImageDescriptor fakeImageDescriptor = new FakeImageDescriptor("fake");
		imageDescriptorSource = new FakeConstantImageDescriptorSource(fakeImageDescriptor);
	}
	
	@Test
	public void savesDirtyEditor() {
		IWorkbenchWindow window = mock(IWorkbenchWindow.class);
		IWorkbenchPage page = mock(IWorkbenchPage.class);
		IEditorPart[] editors = new IEditorPart[] {
				mock(IEditorPart.class)
			};

		stub(window.getActivePage()).toReturn(page);
		stub(page.getDirtyEditors()).toReturn(editors);
		
		SaveAction action = new SaveAction(window, imageDescriptorSource);
		action.run();
		
		verify(editors[0]).doSave(null);
	}

	@Test
	public void savesAllDirtyEditors() {
		IWorkbenchWindow window = mock(IWorkbenchWindow.class);
		IWorkbenchPage page = mock(IWorkbenchPage.class);
		IEditorPart[] editors = new IEditorPart[] {
				mock(IEditorPart.class),
				mock(IEditorPart.class),
				mock(IEditorPart.class),
				mock(IEditorPart.class),
				mock(IEditorPart.class),
				mock(IEditorPart.class),
			};

		stub(window.getActivePage()).toReturn(page);
		stub(page.getDirtyEditors()).toReturn(editors);
		
		SaveAction action = new SaveAction(window, imageDescriptorSource);
		action.run();

		verify(editors[0]).doSave(null);
		verify(editors[1]).doSave(null);
		verify(editors[2]).doSave(null);
		verify(editors[3]).doSave(null);
		verify(editors[4]).doSave(null);
		verify(editors[5]).doSave(null);
	}
}
